package com.ui;

import Client.Client;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;


public class DENGLU extends JDialog {
    public static Client c=new Client();
    private JPanel contentPane;
    private JButton button_login;
    private JButton buttonCancel;
    private JButton button_zhuce;
    private JTextField ID_TextField;
    private JPasswordField passwordField1;
    private JTextArea textArea;

    public JTextArea getTextArea(String str) {
        textArea.append("开始认证\n");
        textArea.append(str);
        //textArea.append("sadsad55\n");
        return textArea;
    }

    public DENGLU() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(button_login);

        button_login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    onOK();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });
        button_zhuce.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                On_zhuce();
            }
        });

        // 点击 X 时调用 onCancel()
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 遇到 ESCAPE 时调用 onCancel()
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() throws SQLException {
        // 在此处添加您的代码
        String U_ID= ID_TextField.getText();
        String U_PASW= passwordField1.getText();

        c.get(U_ID,U_PASW);
        c.start();
        //System.out.println(1);
        if(c.getFlag())
        {
            if(c.getCharge()>=0)
            {
                //dakaiyonghu
                type_1 t=new type_1();
                type_1.c =c;
                //JTextPane textPane1=getTextArea(c.text);
                JTextArea textArea=getTextArea(DENGLU.c.text);
                t.main();
            }
            else{
                Admin_Cen a=new Admin_Cen();
                a.main();
            }
        }
        else{
            System.out.println("error");
        }

        boolean flag=false;
//INSERT INTO u_in(U_ID,U_PAS,U_TYPE,U_Ver)VALUES('kk132','23456',1,'123318')
//        String SQL1= "SELECT U_ID,U_PAS FROM u_in WHERE u_id=+\""+U_ID+"\"and U_PAS=\""+U_PASW+"\";";
//        if(con.execute(SQL1).next())
//        {
//
//            String SQL2= "SELECT U_Pac FROM u_in WHERE u_id=+\""+U_ID+"\";";
//            boolean dou=con.execute(SQL2).next();
//            System.out.println(dou);
//        }

        //dispose();
    }
    private void On_zhuce() {
        // 在此处添加您的代码
        zhuce.main();
        dispose();
    }
    private void onCancel() {
        // 必要时在此处添加您的代码
        dispose();
    }

    public void main() {

        DENGLU dialog = new DENGLU();
        dialog.pack();
        dialog.setVisible(true);

        System.exit(0);
    }
}
